import tkinter
import tkinter.messagebox


def main():
    # 实例化一个对象，建立window窗口
    window = tkinter.Tk()
    # window窗口标题
    window.title("第一个窗口")
    # window 窗口大小
    window.geometry("800x600")

    # 居中计算
    x = window.winfo_screenwidth()
    y = window.winfo_screenheight()
    x = (x - 800) / 2
    y = (y - 600) / 2

    # 设置位置
    window.geometry("%dx%d+%d+%d" % (800, 600, x, y))

    # 输入框
    input1 = tkinter.Entry(window, show="*", font=("Arial", 14))
    input2 = tkinter.Entry(window, show=None, font=("Arial", 14))
    input1.pack()
    input2.pack()

    # butthon 点击按钮事件
    def insrt_point():
        var = input1.get()
        t.insert("insert", var)

    # 创建一个button ， 点击按钮调用insrt_point函数
    btn1 = tkinter.Button(window, text="insert point", width=10, height=2, command=insrt_point)
    btn1.pack()

    # 创建一个text多行文本
    t = tkinter.Text(window, height=3)
    t.pack()

    # Label标签 , textvariable设置值，解注下两行
    # label_text = tkinter.StringVar()
    # label_text.set("111")
    l = tkinter.Label(window, text="4444", bg="green", fg="white", font=("Arial", 12), width=30, height=2)
    l.pack()

    # list列表框
    lb_text = tkinter.StringVar()
    lb_text.set((1, 2, 3, 4))
    lb = tkinter.Listbox(window, listvariable=lb_text)
    lb.pack()

    # radio单选框
    # radio event
    def radio_event():
        tkinter.messagebox.showinfo(title="信息", message="获取值:%d" % rb.get())

    # 设置默认值
    rb = tkinter.IntVar()
    rb.set(2)

    # radio按钮
    tkinter.Radiobutton(window, text="Option A", variable=rb, value=1, command=radio_event).pack()
    tkinter.Radiobutton(window, text="Option B", variable=rb, value=2, command=radio_event).pack()
    tkinter.Radiobutton(window, text="Option C", variable=rb, value=3, command=radio_event).pack()

    # checkbox 多选框
    cb = tkinter.IntVar()
    cb2 = tkinter.IntVar()

    def print_cb():
        tkinter.messagebox.showinfo(title="info", message="val:%d" % cb.get())

    cb_btn = tkinter.Checkbutton(window, text="Java", variable=cb, onvalue=1, offvalue=2, command=print_cb).pack()
    cb_btn2 = tkinter.Checkbutton(window, text="Python", variable=cb2, onvalue=3, offvalue=4, command=print_cb).pack()

    # 画布
    canvas = tkinter.Canvas(window, bg="green", height=200, width=500)
    image_file = tkinter.PhotoImage(file='20.png')
    # 图片锚定点（n图片顶端的中间点位置）放在画布（250,0）坐标处
    canvas.create_image(100, 0, anchor="n", image=image_file)

    x0, y0, x1, y1 = 100, 100, 150, 150
    rect = canvas.create_rectangle(330, 30, 330 + 20, 30 + 20)  # 画矩形正方形
    canvas.pack()

    def moveit():
        canvas.move(rect,2,2)  # 移动正方形rect（也可以改成其他图形名字用以移动一起图形、元素），按每次（x=2, y=2）步长进行移动

    tkinter.Button(window,text="move it" ,command=moveit).pack()

    window.mainloop()


main()
